package com.leilei.serviceProject.domain.strategy.service.rule.tree.impl;

import com.leilei.serviceProject.domain.strategy.model.valobj.RaffleResultCheckVo;
import com.leilei.serviceProject.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.leilei.serviceProject.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.leilei.serviceProject.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author yanglei
 * @description 规则树兜底节点
 * @create 2024/11/19 17:15
 */
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    // 对数据库配置兜底奖品 rule_value:100:user001,user002,user003 用冒号进行切割
    @Override
    public DefaultTreeFactory.TreeLogicResultEntity logic(Long strategyId, String userId, Integer awardId, String ruleValue) {
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} ruleValue:{}", userId, strategyId, awardId, ruleValue);
        String[] ruleValueArray = ruleValue.split(Constants.COLON);
        if (0 == ruleValueArray.length) {
            log.error("规则树过滤-兜底奖品，兜底奖品未配置告警 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
            throw new RuntimeException("兜底奖品未配置 " + ruleValue);
        }
        // 兜底奖品配置
        Integer luckAwardId = Integer.parseInt(ruleValueArray[0]);
        String luckAwardRuleValue = ruleValueArray.length > 1 ? ruleValueArray[1] : "";
        // 兜底奖品返回
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} awardRuleValue:{}", userId, strategyId, luckAwardId, luckAwardRuleValue);
        return DefaultTreeFactory.TreeLogicResultEntity.builder()
                .ruleTreeResultCheck(RaffleResultCheckVo.TAKE_OVER)
                .treeResultData(DefaultTreeFactory.TreeResultData.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(luckAwardRuleValue)
                        .build())
                .build();
    }
}
